﻿using System;
using System.Data;
using System.Web.UI;
using MyWeb.Business;
using MyWeb.Data;

namespace MyWeb.Admin.Usercontrols
{
    public partial class ucGroupProductManager : UserControl
    {
        private readonly GroupProductService _groupProductService = new GroupProductService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int insert = Int32.Parse(Request.QueryString["in"]);
                if (insert == 1)
                {
                    RefreshControl();
                }
                else
                {
                    int id = Int32.Parse(Request.QueryString["id"]);
                    DataTable dt = _groupProductService.GroupProduct_GetById(id);
                    txtName.Text = dt.Rows[0]["name"].ToString();
                    CKEditor1.Text = dt.Rows[0]["content"].ToString();
                    txtImage.Text = dt.Rows[0]["image"].ToString();
                    txtOrder.Text = dt.Rows[0]["order"].ToString();
                    if (Int32.Parse(dt.Rows[0]["status"].ToString()) == 0)
                    {
                        ckCongbo.Checked = true;
                    }
                    else
                    {
                        ckCongbo.Checked = false;
                    }
                }
            }
        }

        private void RefreshControl()
        {
            txtName.Text = string.Empty;
            txtImage.Text = string.Empty;
            txtOrder.Text = string.Empty;
            CKEditor1.Text = string.Empty;
        }

        protected void btnCancelAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/View.aspx?action=ucGroupProductView");
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            int status = ckCongbo.Checked ? 0 : 1;
            var groupProduct = new GroupProductInfo
                                   {
                                       Name = txtName.Text,
                                       Content = CKEditor1.Text,
                                       Image = txtImage.Text,
                                       Order = Int32.Parse(txtOrder.Text),
                                       Status = status
                                   };

            if (Int32.Parse(Request.QueryString["in"]) == 1)
            {
                _groupProductService.GroupProduct_Insert(groupProduct);
            }
            else
            {
                groupProduct.Id = Int32.Parse(Request.QueryString["id"]);
                _groupProductService.GroupProduct_Update(groupProduct);
            }
            RefreshControl();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int status = ckCongbo.Checked ? 0 : 1;
            var groupProduct = new GroupProductInfo
                                   {
                                       Name = txtName.Text,
                                       Content = CKEditor1.Text,
                                       Image = txtImage.Text,
                                       Order = Int32.Parse(txtOrder.Text),
                                       Status = status
                                   };

            if (Int32.Parse(Request.QueryString["in"]) == 1)
            {
                _groupProductService.GroupProduct_Insert(groupProduct);
            }
            else
            {
                groupProduct.Id = Int32.Parse(Request.QueryString["id"]);
                _groupProductService.GroupProduct_Update(groupProduct);
            }
            Response.Redirect("~/Admin/View.aspx?action=ucGroupProductView");
        }
    }
}